var express = require('express');
var router = express.Router();
var {table_model} = require('../utils/model')
var {aesEncrypt, aesDecrypt} = require('../utils/index')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index',
   { 
     title:'后台管理系统',
      name:''
  });
});

router.get('/home',(req,res)=>{
  res.render('home',{
    msg:'wuhan2110'
  })
})


router.get('/login',(req,res)=>{
  var name = req.query.name ? aesDecrypt(req.query.name) :req.query.name;
  res.render('login',{
    name
  })
})

router.get('/register',(req,res)=>{
  res.render('register')
})

router.get('/mine',(req,res)=>{
  res.render('mine')
})


//路由参数
router.get('/todos/:who/:time?',(req,res)=>{
  res.json({
    url:req.url,
    headers:req.headers,
    query:req.query,   //? 拼接的查询参数
    params:req.params,  // : 路由参数 动态路由
    msg:"获取req的参数"
  })
})

// 获取table数据
router.get('/table',(req,res)=>{
  table_model.find({},{})
  .then(result=>{
    res.json({
      code:200,
      msg:'获取table数据成功',
      result:result,
      query:req.query
    })
  })
})

// post 请求 需要用postman
router.post('/submit',(req,res)=>{
  res.json({
    code:200,
    msg:"提交成功",
    body:req.body,
    result:null
  })
})


module.exports = router;
